#include "odn_gfx_Mesh.h"

namespace ouden
{
	namespace gfx
	{
		
		bool ODN_Mesh::Draw()
		{
			
			//TODO: Put into renderer function 
			
			//Bind the mesh's texture 
			glBindTexture(GL_TEXTURE_2D, texture);
			
			//Enable OpenGL draw arrays 
			glEnableClientState(GL_COLOR_ARRAY);
			glEnableClientState(GL_VERTEX_ARRAY);
			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
			glEnableClientState(GL_NORMAL_ARRAY);
			
			//Create the OpenGL draw arrays using the vertex data 
			//TODO: Check if should make colors unsigned bytes instead 
			glColorPointer(4, GL_FLOAT, sizeof(ODN_Vertex), &vertices[0].color);
			glNormalPointer(GL_FLOAT, sizeof(ODN_Vertex), &vertices[0].normal);
			glVertexPointer(3, GL_FLOAT, sizeof(ODN_Vertex), &vertices[0].pos);
			//TODO: Check that this is working with 3 coords instead of 2 and think about making Vector2D class 
			glTexCoordPointer(3, GL_FLOAT, sizeof(ODN_Vertex), &vertices[0].texcoord);
			
			//Draw the triangles using the vertex indices 
			glDrawElements(GL_TRIANGLES, trianglecount * 3, GL_UNSIGNED_SHORT, triangles[0].vertexindices);
			
			//Empty the OpenGL command buffer and ensure everything is completely drawn 
			glFlush();
			
			//Disable OpenGL draw arrays 
			glDisableClientState(GL_COLOR_ARRAY);
			glDisableClientState(GL_VERTEX_ARRAY);
			glDisableClientState (GL_TEXTURE_COORD_ARRAY);
			glDisableClientState(GL_NORMAL_ARRAY );
			
		}
		
	} //namespace gfx 
} //namespace ouden 

